package com.hmall.item.controller;

import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    //分页查询
    @GetMapping("/list")
    public PageDTO<Item> findList(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "5") Integer size) {
        PageDTO<Item> pageDTO = itemService.findList(page, size);
        return pageDTO;
    }

    //根据id查询商品
    @GetMapping("/{id}")
    public Item findById(@PathVariable("id") Long id) {
        return itemService.getById(id);
    }

    //根据id查询商品
    @PostMapping
    public void save(@RequestBody Item item) {
        itemService.save(item);
    }


    //商品状态 1-代表上架 2-代表下架
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id,
                             @PathVariable("status") Integer status) {

        Item item = Item.builder().id(id).status(status).build();
        itemService.updateById(item);
    }

    //修改
    @PutMapping
    public void update(@RequestBody Item item) {
        itemService.updateById(item);
    }


    //根据id删除商品
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Long id) {
        itemService.removeById(id);
    }

}
